#!/usr/bin/python
# coding=utf-8

import pymysql
from fission.source.SourceCode import SourceCode

'''
host = "192.168.10.111"
user = "myadmin"
passwd = "juziyule"
db = "api_test"
port = 3306
charset = "utf8"
# 新建链接
conn = pymysql.connect(
    host=host,
    port=port,
    user=user,
    passwd=passwd,
    db=db,
    charset=charset,
)
# 获取链接
cur = conn.cursor()
# 执行sql
re = cur.execute("SELECT * FROM api_result WHERE api_name = \"/article/list/userfeed\"")
# 返回结果
dfs = cur.fetchall()
# 存放查询结果
sss = []
# 排除异常数据
for row in dfs:
    if row[7] < 1:
        sss.append(row[7])
# 关闭链接
cur.close()
conn.commit()
conn.close()
'''


class MysqlFission:
    host = "10.10.203.64"
    user = "admin"
    passwd = "admin"
    db = "api_fan"
    port = 3306
    charset = "utf8"
    # 新建链接
    conn = pymysql.connect(
        host=host,
        port=port,
        user=user,
        passwd=passwd,
        db=db,
        charset=charset,
    )

    def __init__(self):
        print "数据库读写类！"

    def getWeather(self, name):
        lines = []
        conn = self.conn
        # 获取链接
        cur = conn.cursor()
        cur.execute("SELECT DATE(date),low,high FROM weather WHERE city = \"" + name + "\";")
        data = cur.fetchall()
        for row in data:
            time = row[0]
            low = row[1]
            high = row[2]
            line = str(time) + "|" + str(low) + "|" + str(high)
            lines.append(line)
        SourceCode.savestr("\n".join(lines), name)

    def getApiTimes(self, *params):
        num = str(params.__len__())
        print "接口数：" + num
        conn = self.conn
        # 获取链接
        cur = conn.cursor()
        data = []
        size = []
        for api in params:
            cur.execute("SELECT * FROM api_result WHERE api_name = \"" + api + "\"")
            dfs = cur.fetchall()
            # 排除异常数据
            for row in dfs:
                if row[7] < 1:
                    data.append(row[7])
                    size.append(api)
        cur.close()
        conn.commit()
        conn.close()
        return [data, size]

    def sayHello(self):
        print "hello word!"
        # self.getApiTimes('/article/list/userfeed', '/article/detail', '/article/info')
